<?php


namespace app\api\service;

use EasyWeChat\Kernel\Exceptions\Exception;
use tmcore\base\BaseService;
use tmcore\services\wechat\WxOaService;

class WechatService extends BaseService
{
    /**
     * 获取微信配置
     * @param $params
     * @return array|bool|string
     * @throws \GuzzleHttp\Exception\GuzzleException
     * @throws \Psr\SimpleCache\InvalidArgumentException
     */
    public static function jsConfig($params)
    {
        try {
            $url = urldecode($params['url']);
            if(empty($params['jsApiList'])) {
                $params['jsApiList'] = [
                    'onMenuShareTimeline',
                    'onMenuShareAppMessage',
                    'onMenuShareQQ',
                    'onMenuShareWeibo',
                    'onMenuShareQZone',
                    'openLocation',
                    'getLocation',
                    'chooseWXPay',
                    'updateAppMessageShareData',
                    'updateTimelineShareData',
                    'openAddress',
                    'scanQRCode'
                ];
            } else {
                $params['jsApiList'] = is_array($params['jsApiList'])?$params['jsApiList']:explode(',', $params['jsApiList']);
            }

            $data = (new WxOaService())->getJsConfig($url, $params['jsApiList']);

            return is_string($data)?json_decode($data, true):$data;
        } catch (Exception $e) {
            return self::setError('获取jssdk失败:' . $e->getMessage());
        }
    }
}